# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymysql

class HunterPipeline(object):
    def open_spider(self, spider):
        self.conn = pymysql.Connect(
            host='localhost',
            port=3306,
            user='root',
            password='root',
            db='hunter1',
        )
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        print("存储到DB")
        sql = "insert into data(url, author, title, content, img_url) values (%s, %s, %s, %s, %s)"
        self.cursor.execute(sql, (item["url"], item["author"], item["title"], item["content"], item["img_urls"]))
        self.conn.commit()
        return item

    def close_spider(self, spider):
        self.conn.close()

